<template>
  <div style="margin-left:-12px;margin-top:-12px;width:102.5%;">
    <keep-alive>
      <iframe class="frame-page" :id="id" :src="url" frameborder="0" width="100%" height="800px" scrolling="auto"></iframe>
    </keep-alive>
  </div>
</template>

<script>
import { ACCESS_TOKEN } from "@/store/mutation-types";
import * as tools from '@/utils/util';
import * as hashtoken from "@/api/hashToken/hashtoken.js";

export default {
  name: "IframePageContent",
  inject: ["closeCurrent"],
  data() {
    return {
      url: "",
      id: "",
      loading: false,
    };
  },
  created() {
    this.goUrl();
  },
  updated() {
    this.goUrl();
  },
  computed: {
    userInfo() {
      return this.$store.getters.userInfo;
    }
  },
  watch: {
    $route(to, from) {
      (async () => {
        await this.goUrl(to , from);
      })();
    }
  },
  methods: {
    async goUrl(to , from) {

      //获取URL参数
      var url = this.$route.meta.url;
      var username = this.userInfo.username;
      var date = dayjs().format('YYYY-MM-DD');

      //查询缓存中的token
      var cachetoken = hashtoken.queryToken(date , username);

      //如果是天气预报，则跳转到本地天气预报
      if(this.$route.meta.title == '天气预报'){
        url = window._CONFIG['weatherURL'] || `https://weather.shengtai.club/`;
      }

      //如果存在token关键字段,则计算hash值
      if(url.includes('hashkey')&&url.includes('hashtoken')){

        try {
          //获取stoken
          var stoken = Vue.ls.get(ACCESS_TOKEN);
        } catch (error) {
          console.log(error);
        }

        //设置url token hashkey
        if(!tools.isNull(cachetoken.key)&&!tools.isNull(cachetoken.hash)){
          //从缓存中获取到缓存数据
          url = url.replace('{hashkey}' , cachetoken.key).replace('{hashtoken}',cachetoken.hash).replace('{token}',tools.isNull(stoken) ? cachetoken.stoken : stoken).replace('{fingerprint}',cachetoken.fingerprint);
        } else {
          //设置url/token/hashkey
          cachetoken = hashtoken.calcuToken(cachetoken.key, cachetoken.hash, date, username);
          //设置url信息
          url = url.replace('{hashkey}' , cachetoken.key).replace('{hashtoken}',cachetoken.hash).replace('{token}',tools.isNull(stoken) ? cachetoken.stoken : stoken).replace('{fingerprint}',cachetoken.fingerprint);
        }

        //添加管理员标识，如果用户具有管理员权限，则添加管理员标识
        try {
          let role = await window.queryAdminUserByView(username);
          if(url.includes('username=common')){
            url = role.length === 0 ?  `${url}&flag=common` : `${url}&flag=admin`;
          } else {
            url = role.length === 0 ?  `${url}&flag=admin` : `${url}&flag=admin`;
          }
        } catch (error) {
          console.log(error);
        }

      }

      //设置用户名称
      url = url.replace('{username}' , username);

      let id = this.$route.path;
      
      this.id = id;

      if (url !== null && url !== undefined) {
        this.url = url;
        if (
          this.$route.meta.internalOrExternal != undefined &&
          this.$route.meta.internalOrExternal == true
        ) {
          this.closeCurrent();
          //外部url加入token
          let tokenStr = "${token}";
          if (url.indexOf(tokenStr) != -1) {
            try {
              let token = Vue.ls.get(ACCESS_TOKEN);
              this.url = url.replace(tokenStr, token);
            } catch (error) {
              console.log(error);
            }
          }
          window.open(this.url);
        }
        if (url.includes('https://pan') && this.loading == false) {
          this.loading = true;
        }
      }
    }
  }
};
</script>

<style>
</style>